package cn.bonoon.kernel.web.handlers;

import cn.bonoon.handler.AutoManager;
import cn.bonoon.kernel.events.OperateEvent;
import cn.bonoon.kernel.support.services.OperateService;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.controllers.GridSelectedType;
import cn.bonoon.kernel.web.controllers.GridToolbarResolver;
import cn.bonoon.kernel.web.controllers.StatusButtonResolver;
import cn.bonoon.kernel.web.models.IdsParameter;

public class OperateIdsHandler extends AbstractOperateAjaxHandler{

	private OperateIdsHandler(int operand, OperateService operateService, AutoManager initializer) {
		super(operand, operateService, initializer);
	}
	
	public OperateIdsHandler(int operand, String name, OperateService operateService, AutoManager initializer) {
		super(operand, name, operateService, initializer);
	}

	@Override
	public IdsParameter buildParams() {
		return new IdsParameter();
	}
	
	@Override
	protected Object internalExecute(OperateEvent event) {
		return operateService.operate(operandCode, event, IdsParameter.toIds(event));
	}
	
	@Override
	public StatusButtonResolver register(StatusButtonResolver resolver) {
		resolver.setEventType(ButtonEventType.POST);
		return super.register(resolver);
	}

	public static GridToolbarResolver toolbar(HandlerRegister register, OperateIdsHandler handler, String icon) throws Exception{
		GridToolbarResolver tr = register.toolbar();
		handler.register(tr);
		tr.setIconCls(icon);
		tr.selectedType(GridSelectedType.MULTIPLE);
		return tr;
	}
	
	public static GridToolbarResolver toolbar_clear(HandlerRegister register, OperateIdsHandler handler, String icon) throws Exception{
		GridToolbarResolver tr = register.toolbar();
		handler.register(tr);
		tr.setIconCls(icon);
		tr.selectedType(GridSelectedType.EMPTY);
		return tr;
	}
	/**
	 * 向toolbar上追加"删除"按钮
	 * @param register
	 * @param operateService
	 * @return
	 * @throws Exception
	 */
	public static GridToolbarResolver delete(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdsHandler handler = new OperateIdsHandler(OPERAND_DELETE, operateService, register.getManager());
		return toolbar(register, handler, "icon-del");
	}
	/**
	 * 向toolbar上追加"禁止"按钮
	 * @param register
	 * @param operateService
	 * @return
	 * @throws Exception
	 */
	public static GridToolbarResolver forbid(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdsHandler handler = new OperateIdsHandler(OPERAND_FORBID, operateService, register.getManager());
		return toolbar(register, handler, "icon-forbid");
	}
	/**
	 * 向toolbar上追加"恢复"按钮
	 * @param register
	 * @param operateService
	 * @return
	 * @throws Exception
	 */
	public static GridToolbarResolver resume(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdsHandler handler = new OperateIdsHandler(OPERAND_RESUME, operateService, register.getManager());
		return toolbar(register, handler, "icon-accept");
	}
}
